package com.atguigu.dao.impl;

import com.atguigu.dao.OrderDao;
import com.atguigu.pojo.Order;

import java.util.List;

public class OrderDaoImpl extends BaseDao implements OrderDao {
    /**
     *    保存订单
     * @param order
     * @return
     */
    @Override
    public int saveOrder(Order order) {
        String sql="insert into t_order(order_id,create_time,price,status,user_id) values(?,?,?,?,?)";
        int update = update(sql, order.getOrderId(), order.getCreateTime(), order.getPrice(), order.getStatus(), order.getUserId());
        return update;
    }

    /**
     * 查询所有的订单信息
     * @return
     */
    @Override
    public List<Order> queryOrders() {
        String sql="SELECT order_id orderId,create_time createTime,price,status,user_id userId FROM t_order";
        return  queryForList(Order.class,sql);
    }

    /**
     * 根据订单单号修改status的状态
     * @param orderId
     * @param status
     * @return
     */
    @Override
    public int changeStatus(String orderId, Integer status) {
        String sql="UPDATE t_order SET STATUS=? WHERE order_id=?";
         update(sql,status,orderId);
         return status;
    }

    /**
     * 根据书的编号查找订单信息
     * @param userId
     * @return
     */
    @Override
    public List<Order> queryOrdersByUserId(Integer userId) {
        String sql="SELECT order_id orderId,create_time createTime,price,status,user_id userId FROM t_order where user_id=?";
        return queryForList(Order.class,sql,userId);
    }
}
